﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using AForge.Imaging.Filters;

namespace Ession.OCR
{
    public class ImageTool
    {
        static public Bitmap BlackWhite(Bitmap source)
        {
            Bitmap temp = source.Clone() as Bitmap; // 复制原始图像

            FiltersSequence seq = new FiltersSequence();
            seq.Add(Grayscale.CommonAlgorithms.BT709);  // 添加灰度滤镜
            seq.Add(new OtsuThreshold()); // 添加二值化滤镜
            return seq.Apply(source); // 应用滤镜
        }

        static public Bitmap ReverseBlackWhite(Bitmap source)
        {
            Bitmap temp = source.Clone() as Bitmap; // 复制原始图像

            for (int i = 0; i < temp.Width; i++)
            {
                for (int j = 0; j < temp.Height; j++)
                {
                    var c = temp.GetPixel(i, j);
                    if (c.R<250)//黑
                    {
                        temp.SetPixel(i, j, Color.White);
                    }
                    else
                    {
                        temp.SetPixel(i, j, Color.Black);
                        
                    }
                }
            }
            return temp;
        }
    }
}
